/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.faces.context.FacesContext;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Speciality;
import net.gis.eemc.service.FacultyServiceImpl;
import net.gis.eemc.service.DepartmentServiceImpl;
import net.gis.eemc.service.SpecialityServiceImpl;

/**
 *
 * @author Sergey
 */
@ManagedBean(name = "specialitiesBean")
@ViewScoped
public class SpecialitiesBean {

    private final String allFaculties = "Все";
    private final String allDepartments = "Все";
    List<Speciality> specialities = null;
    private Integer selectedFaculty = null;
    private Integer selectedDepartment = null;
    private List<SelectItem> selectFaculties = null;
    private Map<Integer, Faculty> facultyMap = null;
    private List<SelectItem> selectDepartments = null;
    private Map<Integer, Department> departmentMap = null;
    private FacultyServiceImpl facSrv = null;
    private DepartmentServiceImpl depSrv = null;
    private SpecialityServiceImpl specSrv = null;

    private synchronized void fillSelectFaculties() {
        selectFaculties = new ArrayList<SelectItem>();
        facultyMap = new HashMap<Integer, Faculty>();
        selectFaculties.add(new SelectItem(0, allFaculties));
        
        for (Faculty fac : facSrv.getFaculties()) {
            facultyMap.put(fac.getIdfaculty(), fac);
            selectFaculties.add(new SelectItem(fac.getIdfaculty(), fac.getShortName()));
        }
    }

    private synchronized void fillSelectDepartments() {
        if (selectedFaculty != null) {
            selectDepartments = new ArrayList<SelectItem>();
            departmentMap = new HashMap<Integer, Department>();
            selectDepartments.add(new SelectItem(0, allDepartments));

            for (Department dep : depSrv.getDepartmentsForFaculty(selectedFaculty)) {
                departmentMap.put(dep.getIdDep(), dep);
                selectDepartments.add(new SelectItem(dep.getIdDep(), dep.getShortName()));
            }
        } else {
            selectedDepartment = null;
            departmentMap = null;
        }
    }

    private synchronized void fillSpecList() {
        if (selectedDepartment != null && selectedDepartment != 0) {
            specialities = specSrv.getSpecialities(departmentMap.get(selectedDepartment));
        } else if (selectedFaculty != null && selectedFaculty != 0) {
            specialities = specSrv.getSpecialities(facultyMap.get(selectedFaculty));
        } else {
            specialities = specSrv.getSpecialities();
        }
    }

    /** Creates a new instance of SpecialitiesBean */
    public SpecialitiesBean() {
        try {
            facSrv = new FacultyServiceImpl();
            depSrv = new DepartmentServiceImpl();
            specSrv = new SpecialityServiceImpl();

            fillSelectFaculties();
            
            FacesContext context = FacesContext.getCurrentInstance();
            String id = context.getExternalContext().getRequestParameterMap().get("id");
            if (id != null) {
                selectedDepartment = new Integer(id);
                selectedFaculty = depSrv.getDepartments(selectedDepartment).getFaculty().getIdfaculty();
            } else {
                selectedDepartment = null;
                selectedFaculty = null;
            }

            fillSelectDepartments();

            fillSpecList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Speciality> getSpecialities() {
        return specialities;
    }

    public Integer getSelectedFaculty() {
        return selectedFaculty;
    }

    public void setSelectedFaculty(Integer faculty) {
        this.selectedFaculty = faculty;
        this.selectedDepartment = null;
        fillSelectDepartments();
        fillSpecList();
    }

    public Integer getSelectedDepartment() {
        return selectedDepartment;
    }

    public void setSelectedDepartment(Integer department) {
        this.selectedDepartment = department;
        fillSpecList();
    }

    public List<SelectItem> getSelectFaculties() {
        return selectFaculties;
    }

    public List<SelectItem> getSelectDepartments() {
        return selectDepartments;
    }
}
